[Israel.pm] Help: why does it warn me?

sawyer x xsawyerx at gmail.com
Wed Feb 4 08:00:28 PST 2009


Probably a late response, BUT:
1. mjd's advice on "my" and "local" is: "always use my, never use
local", even though he declares that here is one or two exceptions.
Strictly speaking, you should ONLY use local when you want to change a
global variable for a local code block. For example, the $/ variable
(when you "use English '-no_match_vars'" it's called $RS or
$INPUT_RECORD_SEPARATOR) indicates when to stop reading a record. When
trying to read from a file to a scalar, it stops after the first line
because $/ is set to "\n" so after the first line, it stops. If you
want to read the entire line to a scalar, you need to change that. You
probably don't want to ruin a different part of the program (since
this is a global variable), so you change that global variable to a
local part:

sub read_file {
    local $/ = undef; # setting it to undefined, so it will never stop
reading until the end of the file
    my $filename = shift;
    open my $fh, '<', $filename or die "can't open $filename: $!\n";
    my $content = <$fh>; # just read the entire file into one
variable, because $/ is no longer in the way
    close $fh;
    return $content;
}

Then used somewhere:
my $file_content = read_file($filename);

2. Hashes - at least in your case - have a few benefits:
- Much less code to write
- Much cleaner to read and work on
- Much more unified and flexible
- Much easier to understand, really

So don't fear the Hash :)

On Wed, Feb 4, 2009 at 5:35 PM, Chanan Berler <chananb at centerity.com> wrote:
>
> Thanks for the help,
>
> Using the array as return might work for me....more cleaner and next time I
> will look at it, it will pop right off
> Using hash although will work for me, it will be hard to understand
>
> But thanks I will take ur advice
> Chanan
>
> -----Original Message-----
> From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
> Of Gabor Szabo
> Sent: Wednesday, February 04, 2009 5:32 PM
> To: Perl in Israel
> Subject: Re: [Israel.pm] Help: why does it warn me?
>
> why not write:
>
>
>  sub update_event_log
>  {
>    my ($lv_reg) = @_;
>
>    my ( $lv_attemp_stmt, $lv_service_stmt, $lv_host_stmt,
> $lv_hostgroup_stmt,  $lv_servicegroup_stmt)  =
>    _el_parse_reg_($lv_reg);
>
>  }
>
> and let the _el_parse_reg_() return the values?
>
> or even cleaner
>
>  sub update_event_log
>  {
>    my ($lv_reg) = @_;
>
>    my %lv  =  _el_parse_reg_($lv_reg);
>  }
>
> and have
>    attemp, service, host,  hostgroup and servicegroup be the keys of that
> hash.
>
>
> You should nearly never use anything else than "my".
>
>
> Gabor
>
>
> On Wed, Feb 4, 2009 at 4:58 PM, Chanan Berler <chananb at centerity.com> wrote:
> > Hi All,
> >
> > I wrote this script:
> >
> >
> > sub update_event_log
> > {
> >    my ($lv_reg) = @_;
> >
> >    local $lv_attemp_stmt;
> >    local $lv_service_stmt;
> >    local $lv_host_stmt;
> >    local $lv_hostgroup_stmt;
> >    local $lv_servicegroup_stmt;
> >
> >    &_el_parse_reg_($lv_reg);
> >
> > }
> >
> > The idea that sub el_parse_reg will update these fields.
> > I  tired doing something like that as a test (but without warnings) and it
> > worked.
> > But when I tried to imply use warnings and use strict I get this kind of
> > warning / error message:
> >
> > Global symbol "$lv_host_stmt" requires explicit package name at
> event_log.pl
> > line 162.
> >
> > Why is that?
> > Thanks
> > Chanan
> >
> > PS: correct me if I am wrong
> >  But as what I understood:
> >
> > my - applies to sub private variables
> > local - protected scalars that can be updated by other sub called by the
> sub
> > which created the local variables
> > our - more like a global like variables - which can be accessed from all
> the
> > module / script found on that file
> >
> >
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl



More information about the Perl mailing list