Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

What is wrong with variable?

by w3b (Beadle)
on Mar 18, 2006 at 16:57 UTC ( [id://537683]=perlquestion: print w/replies, xml ) Need Help??

w3b has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Monks!I wrote a script to run commands in linux via e-mail/sms.I think that all code is ok... but in 28 line i have error: Use of uninitialized value in numeric lt (<) at cbe.pl line 28. I don't have any idea, what i did wrong.
#!/usr/bin/perl use warnings; use strict; use Net::POP3; my $pop = Net::POP3->new('pop3.server') || die("Can't connect"); if(!$pop->login('email@adress.com', 'password')){ die("Can't connect"); } my $now = '0'; sub check { my $last = shift; $pop->quit; sleep 5; $now = $pop->login('email@adress.com', 'password'); if ( $last < $now){ # <- WRONG my $msg = $pop->get($now); for my $line (@$msg){ if( $line =~ /^Subject: `(.*)`/ ){ system(`$1`); last } } } return $now; } while (1){ &check($now); } $pop->quit();
I want to add next function but, this function have to run correctly. Thx

Replies are listed 'Best First'.
Re: What is wrong with variable?
by rafl (Friar) on Mar 18, 2006 at 17:03 UTC

    Apparently one of the variables ($last or $now) is undefined.

    I think the reason for that is, that

    $pop->login(..);

    may return undef to indicate that the login attempt failed. You check it in line 9, where you log in the first time, but you don't do that for any later calls of the login method.

    Cheers, Flo

Re: What is wrong with variable?
by dws (Chancellor) on Mar 19, 2006 at 01:01 UTC
    if( $line =~ /^Subject: `(.*)`/ ){ system(`$1`); last }

    I hope that's an extract from a script that does some extract checking, because if you're blindly executing commands that come in on subject lines to emails, you are setting yourself up for some major hurt. I've seen random spam come in with backquotes around the subject body.

      It's test version, if it work i'll add authorization function...
        Authorization is the second step, before that you will need an authentication check to make sure that the one who logs in is indeed who (s)he says (s)he is (that is the usual login-id and password stuff).

        It is a bit difficult to see how you can authenticate through e-mail (perhaps putting the log-in and password in the body of the e-mail? But that is not secure at all as anybody who intercepts the e-mail will know your log-in and password). Simply checking for the e-mail address of the sender is of course not enough as e-mail headers are far too easily spoofed. Anyone who has taken the Spam 101-course will know how to do it.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: What is wrong with variable?
by qbxk (Friar) on Mar 19, 2006 at 10:12 UTC
    just my 2c, but i don't think this is cool:

    my $now = '0';
    (the quotes)

    It's not what you look like, when you're doin' what you’re doin'.
    It's what you’re doin' when you’re doin' what you look like you’re doin'!
         - Charles Wright & the Watts 103rd Street Rhythm Band
Re: What is wrong with variable?
by w3b (Beadle) on Mar 19, 2006 at 05:06 UTC
    .... did you see: i closed connection and run function login.... Now it's work:
    #!/usr/bin/perl use warnings; use strict; use Net::POP3; my $pop = Net::POP3->new('pop3.server') || die("Coudn't connect"); if(!$pop->login('email@adress.com', 'password') < 0){ die("Can't connect"); } my $last, $now) = '0'; sub check { $last = shift; $pop->quit; sleep 5; $pop = Net::POP3->new('pop3.server') || die("Can't connect"); $now = $pop->login('email@adress', 'password') || die("Can't c +onnect"); if ($last < $now){ my $msg = $pop->get($now); for my $line (@$msg){ if( $line =~ /^Subject: '(.*)'/ ){ system(`$1`); return $now; print "I done $1"; last } } } return $now; } while (1){ &check($now); } $pop->quit();
    Now I can add next function =] I love perl=] greetz & thx
      so the posted code works?
      my $last, $now)= '0';
      What is this supposed to do? Initialize both $last and $now to '0'? Guess again...
      my $last = my $now = '0';
      works better ;-)

      btw, what is this supposed to do:

      system(`$1`); return $now; print "I done $1"; last
      The last two lines will never get executed... never! BTW: why do you use backticks in your system() sub? It's either `$1`, or do{ open(my $cmdh, '-|', $1); <$cmdh> } or system($1)... but combining two of them is probably not what you want to do...
      and... you might wanna write those 4 lines in a different order:
      `$1`; print qq{I've done $1}; return $now
      3 lines... does the same thing ;)

      cya!

      to ask a question is a moment of shame
      to remain ignorant is a lifelong shame

        I wrote bad version of script... now i see.... I'll copy good version today... sorry

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://537683]
Approved by idsfa
Front-paged by astaines
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found