Professional Web Applications Themes

confused - if function - PERL Beginners

I am getting a bit confused here, if I uncomment out the print statement within the if block $date1 prints, however the $date1 after the if block doesn't if ($date eq ''){ my $date1=localtime; #print $date1; } else { my $date1=~$date; }; print $date1; any ideas...

  1. #1

    Default confused - if function

    I am getting a bit confused here, if I uncomment out the print statement
    within the if block $date1 prints, however the $date1 after the if block
    doesn't

    if ($date eq ''){
    my $date1=localtime;
    #print $date1;
    }
    else {
    my $date1=~$date;
    };
    print $date1;

    any ideas
    Mike Guest

  2. #2

    Default Re: confused - if function

    On Apr 27, 2004, at 5:39 AM, mike wrote:
     

    my(...) scopes a variable locally, which usually means it ceases to
    exist at the next }, or end of the block. If you need the same
    variable through and outside a block, you have to declare it outside:

    my $date1;
    if ($date eq '') { $date1 = localtime; }
    else { $date1 = $date; } # not what you had, but I bet it's what you
    meant
    print "$date1\n";

    While we're talking, you should look into adding the following two
    lines to the top of all your code:

    use strict;
    use warnings;

    This would have kept the code you posted from compiling and it would
    have told you why. Build good habits.

    Good luck.

    James

    James Guest

  3. #3

    Default Re: confused - if function





    It works, but it prints nothing. You are basically dealing with 3 variable
    here. the first 2 $date1 goes out of scope outside if {} else {}.

    Simply move your declaration out of the if statement. That is:


    my $date1 = "";


    if ( your condition){
    $date1=localtime;
    }
    else {
    ..
    ..

    };

    print $date1;


    __________________

    William Ampeh (x3939)
    Federal Reserve Board



    mike
    <mikebristolrec To: org
    cc.co.uk> cc:
    Subject: confused - if function
    04/27/2004 06:39
    AM






    I am getting a bit confused here, if I uncomment out the print statement
    within the if block $date1 prints, however the $date1 after the if block
    doesn't

    if ($date eq ''){
    my $date1=localtime;
    #print $date1;
    }
    else {
    my $date1=~$date;
    };
    print $date1;

    any ideas

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>





    William Guest

  4. #4

    Default Re: confused - if function

    You are absolutely correct, that the my is included inside the { } block. But that is exactly where the confusion sets in, having had this problem when I started coding in perl. The { } are required when using an if/else construct -- part of the syntax to perform this function. Whereas a block is *optional*, like in C/C++.

    So yes, technically, any code inside a { } will be a block and the 'my' looses scope. But I totally understand the original poster's confusion!


    -JW



    James Edward Gray II <net> wrote:
    On Apr 27, 2004, at 5:39 AM, mike wrote:
     

    my(...) scopes a variable locally, which usually means it ceases to
    exist at the next }, or end of the block. If you need the same
    variable through and outside a block, you have to declare it outside:

    my $date1;
    if ($date eq '') { $date1 = localtime; }
    else { $date1 = $date; } # not what you had, but I bet it's what you
    meant
    print "$date1\n";

    While we're talking, you should look into adding the following two
    lines to the top of all your code:

    use strict;
    use warnings;

    This would have kept the code you posted from compiling and it would
    have told you why. Build good habits.

    Good luck.

    James


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org




    ---------------------------------
    Do you Yahoo!?
    Win a $20,000 Career Makeover at Yahoo! HotJobs
    Jeff Guest

Similar Threads

  1. confused
    By jojo in forum Macromedia Dreamweaver
    Replies: 14
    Last Post: April 20th, 05:09 PM
  2. Confused with FCS And FMS
    By CubanAplanos in forum Macromedia Flash Flashcom
    Replies: 2
    Last Post: February 18th, 04:42 AM
  3. OT: :confused;
    By Drew in forum Dreamweaver AppDev
    Replies: 2
    Last Post: April 15th, 05:06 PM
  4. Very new - Very confused
    By Jim in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: August 4th, 02:05 PM
  5. Confused....
    By kevin_tx webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 10th, 05:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139