PDA

View Full Version : Can't call method on an undefined value


NIIcK
07-04-2008, 10:41 PM
Hello,

I am really new to Perl/CGI. I have got this script that I'm trying to install but it gives me an error:

Can't call method "f" on an undefined value.

The lines related to this error are:

my $ses = Session->new;
my $f = $ses->f;

I appreciate any help you can offer.

Thank you,

Nick

FishMonger
07-04-2008, 10:47 PM
The error is telling you that $ses is undefined, which would mean that Session->new failed.

You'll need to post more of your code.

NIIcK
07-04-2008, 10:50 PM
Here it is the entire code:


#!/usr/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use lib '.';
use DBI;
use XFileConfig;
use Session;
use CGI qw(param);
my $ok = "<br><b style='background:#1a1;color:#fff;padding:2px;'>OK</b>";

my $ses = Session->new;
my $f = $ses->f;

if($f->{site_settings})
{
my @fields = qw(temp_dir upload_dir htdocs_dir);
$f->{temp_dir} = "$f->{cgi_path}/temp";
$f->{upload_dir} = "$f->{cgi_path}/uploads";
$f->{htdocs_dir} = "$f->{site_path}/files";
my $conf;
open(F,"XFSConfig.pm")||$ses->message("Can't read XFSConfig");
$conf.=$_ while <F>;
close F;
for my $x (@fields)
{
my $val = $f->{$x};
$conf=~s/$x\s*=>\s*(\S+)\s*,/"$x => '$val',"/e;
}
open(F,">XFSConfig.pm")||$ses->message("Can't write XFSConfig");
print F $conf;
close F;
}

if($f->{save_sql_settings} || $f->{site_settings})
{
my @fields = $f->{save_sql_settings} ? qw(db_host db_login db_passwd db_name pasword_salt) : qw(site_url site_cgi site_path);
my $conf;
open(F,"XFileConfig.pm")||$ses->message("Can't read XFileConfig");
$conf.=$_ while <F>;
close F;
$f->{pasword_salt} = $ses->randchar(12);
$f->{dl_key} = $ses->randchar(10);
for my $x (@fields)
{
my $val = $f->{$x};
$conf=~s/$x\s*=>\s*(\S+)\s*,/"$x => '$val',"/e;
}
open(F,">XFileConfig.pm")||$ses->message("Can't write XFileConfig");
print F $conf;
close F;
$ses->redirect('install.cgi');
}

if($f->{create_sql})
{
my $db = $ses->db;
open(FILE,"install.sql")||$ses->message("Can't open create.sql");
my $sql;
$sql.=$_ while <FILE>;
$sql=~s/CREATE TABLE/CREATE TABLE IF NOT EXISTS/gis;
$db->Exec($_) for split(';',$sql);
$db->Exec("INSERT INTO Users (usr_login,usr_email,usr_password,usr_created,usr_adm) VALUES (?,?,ENCODE(?,?),NOW(),1)",$f->{usr_login},$f->{usr_email},$f->{usr_password},$c->{pasword_salt});
$ses->redirect('install.cgi');
}


print"Content-type:text/html\n\n";
print"<HTML><BODY style='font:13px Arial;'><h2>XFileSharingPro Installation Script</h2>";
print"<b>1) Permissions Check</b><br><br>";
my $perms = {
'logs.txt' => 0666,
'ipn_log.txt' => 0666,
'fs.cgi' => 0755,
'index.cgi' => 0755,
'ipn.cgi' => 0755,
'cron.pl' => 0755,
'dl.pl' => 0755,
'upload.cgi' => 0755,
'upload_status.cgi' => 0755,
'api.cgi' => 0755,
'XFileConfig.pm' => 0666,
'XFSConfig.pm' => 0666,
'temp' => 0777,
'uploads' => 0777,
"$c->{site_path}/files" => 0777,
"$c->{site_path}/captchas" => 0777,
};
my @arr;
for(keys %{$perms})
{
next unless -e $_;
chmod $perms->{$_}, $_;
my $chmod = (stat($_))[2] & 07777;
my $chmod_txt = sprintf("%04o", $chmod);
push @arr, "<b>$_</b> : $chmod_txt : ".( $chmod == $perms->{$_} ? 'OK' : "<u>ERROR: should be ".sprintf("%04o",$perms->{$_})."</u>" );
}

chmod 0666, "$c->{site_path}/.htaccess" if -f "$c->{site_path}/.htaccess";
print join '<br>', @arr;
if( grep{/ERROR/}@arr )
{
print"<br><br><u>Fix errors above and refresh this page</u>";
}
else
{
print"<br><br>All permissions are correct.$ok";
}


print"<hr>";

print"<b>2) MySQL Settings</b><br><br>";
my $dbh=DBI->connect("DBI:mysql:database=$c->{db_name};host=$c->{db_host}",$c->{db_login},$c->{db_passwd}) if $c->{db_name} && $c->{db_host};
if($dbh)
{
print"MySQL Settings are correct. Can connect to DB.$ok";
}
else
{
print<<EOP
Can't connect to DB with current settings: $DBI::errstr<br><br>
<Form method="POST">
<input type="hidden" name="save_sql_settings" value="1">
MySQL Host:<br>
<input type="text" name="db_host" value="$c->{db_host}"><br>
MySQL DB Username:<br>
<input type="text" name="db_login" value="$c->{db_login}"><br>
MySQL DB Password:<br>
<input type="text" name="db_passwd" value="$c->{db_passwd}"><br>
MySQL DB Name:<br>
<input type="text" name="db_name" value="$c->{db_name}"><br><br>
<input type="submit" value="Save MySQL Settings">
</Form>
EOP
;
}

print"<hr>";

print"<b>3) MySQL tables create & Admin account</b><br><br>";

if(!$dbh)
{
print"Fix MySQL settings above first.";
}
else
{
my $sth=$dbh->prepare("DESC Files");
my $rc=$sth->execute();
if($rc)
{
print"Tables created successfully.$ok";
}
else
{
print<<EOP
<form method="POST">
<input type="hidden" name="create_sql" value="1">
Admin login:<br><input type="text" name="usr_login"><br>
Admin password:<br><input type="text" name="usr_password"><br>
Admin E-mail:<br><input type="text" name="usr_email"><br><br>
<input type="submit" value="Create MySQL Tables & Admin Account">
</form>
EOP
;
}
}

print"<hr><b>4) Site URL / Path Settings</b><br><br>";
if($c->{site_url} && $c->{site_cgi} && -d $c->{site_path})
{
print"Settings are correct.$ok";
print"<br><br><b>Login as admin and add FileServer now: <a href='$c->{site_url}/login.html'>$c->{site_url}/login.html</a></b>";
}
else
{
my $path = $ENV{DOCUMENT_ROOT};
my ($cgipath) = $ENV{SCRIPT_FILENAME}=~/^(.+)\//;
my $url_cgi = 'http://'.$ENV{HTTP_HOST}.$ENV{REQUEST_URI};
$url_cgi=~s/\/[^\/]+$//;
my $url = 'http://'.$ENV{HTTP_HOST};

$url = $c->{site_url}||$url;
$url_cgi = $c->{site_cgi}||$url_cgi;
$path = $c->{site_path}||$path;
print<<EOP
<form method="POST">
<input type="hidden" name="site_settings" value="1">
htdocs folder URL:<br>
<input type="text" name="site_url" value="$url" size=48> <small>No trailing slash</small><br>
cgi-bin folder URL:<br>
<input type="text" name="site_cgi" value="$url_cgi" size=48> <small>No trailing slash</small><br>
cgi-bin disk path:<br>
<input type="text" name="cgi_path" value="$cgipath" size=48><br>
htdocs disk path:<br>
<input type="text" name="site_path" value="$path" size=48><br>
<br>
<input type="submit" value="Save site settings">
</form>
EOP
;

}

print"<hr><b>5) Manually Remove install files</b><br><br>install.cgi<br>install.sql<br>convert.cgi<br>convert.sql";


Thank you for your quick reply!

Nick

FishMonger
07-04-2008, 11:37 PM
There are 2 problems that I see, and possibly more.

1) The constructor ( i.e Session->new ) appears to require 2 parameters, the first could be undef.

2) I don't see a method f in Session or its dependencies. So, even if you pass the proper params to the constructor, $ses->f should fail because there is no corresponding method, as far as I can see.

NIIcK
07-05-2008, 12:07 AM
Hmm ... thank you for your time! I think I'll go for a similar PHP one ... for some reason I've always had issues with Perl.

Thank you for your time and help!

Nick

serge
12-30-2009, 09:23 PM
Hi

I am new to perl and using windows. The same message is showing up in my terminal window(Can't call method "value" on an undefined value at c:/perl/lib/WWW/Mechanize.pm ) when i ran my code. My goal is to loop through all the the years starting from 1898 to today. The code can only run for jan1 1898 and shows me the error message

Thank you,

Serge

This is my code

use strict;

use WWW::Mechanize; #use mechanize module

use Date::Simple ('date', 'today');



my $startPage = "http://bub2.meteo.psu.edu/WXSTN/wxstn.htm"; #declare variable #$startPage


my $mech = WWW::Mechanize->new( autocheck => 1 ); #declare variable $mech as #web browser



$mech->get( $startPage ); #opens website http://bub2
#.meteo.psu.edu/WXSTN/wxstn.htm

$mech->form_number('0'); #number of form.


my $todaysdate = today();
my $processdate = Date::Simple->new('1896-01-01');
my $diff = 0;
my $i;


$diff = date($todaysdate) - date($processdate); # Difference in days between two dates



for ($i=0; $i<$diff; $i++)
{
$processdate =~ s/\-//g;

$mech->field('dtg', $processdate); #enter into field.


my $results = $mech->submit(); # click submit on mech browser.


print $results->content(); #print website results onto screen.


$results->content() =~ m/High Temperature\D+(\d+)/; #regular expression that picks #out the high temperature from $results->content();
print "Max Temperature is $1\n"; # and prints it to screen


$results->content() =~ m/Low Temperature\D+(\d+)/; #regular expression that picks #out the Low temperature from $results->content();
print "Min Temperature is $1\n"; # and prints it to screen


print $i;


$processdate = $processdate + 1;


}