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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Can't "last" outside a loop block
by talexb (Chancellor) on Apr 02, 2009 at 17:08 UTC

    Your Perlmonks-fu needs work. Your post currently says this:

      Hi, i get the error "Can't "last" outside a loop block" and ""syntax error at line 1 : `end of file' unexpected"" when i run the below code. any idea about this...??? < if (-e "testfile") { $check = 0; print "testfile is present\n"; } else { $check = 1; $msg1= "testFile Is Not Avaliable. $timex"; $subj1="testFile Is Not Avaliable "; insertlog($msg1); maildl($msg1, $env, $subj1, $maillst); last; } >

    This is, essentially, useless. You should have enclosed your code inside code tags, and this *should* have been obvious when you *previewed* your post.

    It should have looked something like this:

    if ( -e "testfile" ) { $check = 0; print "testfile is present\n"; } else { $check = 1; $msg1 = "testFile Is Not Avaliable. $timex"; $subj1 = "testFile Is Not Avaliable "; insertlog($msg1); maildl( $msg1, $env, $subj1, $maillst ); last; }

    That's just cutting and pasting your code and running it through perltidy. At least now we have a vague idea of what your code looks like. And as it stands, the last isn't necessary -- there's no loop to break out of.

    But if there's more to your code, show it to us. Better than that, of course, would be the smallest possible example where the Weird Behaviour is still seen. That way we don't have to paw through dozens (or hundreds) of lines of irrelevant code.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Can't "last" outside a loop block
by cdarke (Prior) on Apr 02, 2009 at 14:08 UTC
    if this condition is false the script should quit else it should proceed

    That is not what 'last' is for.

    To exit a script use exit, perverse, I know. Or maybe you want to die.
Re: Can't "last" outside a loop block
by JavaFan (Canon) on Apr 02, 2009 at 13:34 UTC
    What's the point of your 'last'? Or, the last of what?. If you're not looping over something, the concept of next or last doesn't make any sense.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Can't "last" outside a loop block
by moritz (Cardinal) on Apr 02, 2009 at 13:37 UTC
    The code you posted doesn't contain a syntax error (as far as I was able to extract it).
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Can't "last" outside a loop block
by lakshmananindia (Chaplain) on Apr 02, 2009 at 13:35 UTC

    last cant be used to exit blocks. Refer perldoc -f last.
    Moreover use <code> tage to post codes

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      last cant be used to exit blocks.

      Wrong (well, half wrong ;-)

      perl -wle '$x = 1; {$x = 2; last; $x = 3} print $x' 2

      The documentation for last states that

      "last" cannot be used to exit a block which returns a value such as "eval {}", "sub {}" or "do {}"

      but it can be used to exit all other loop blocks, even bare blocks. But it can not be used to exit if/elsif/else blocks.

        ...all of which is a pretext for the poor mans case/switch statement illustrated here

        A user level that continues to overstate my experience :-))