Re: perl hash utilities

Discussion in 'Perl Misc' started by Uri Guttman, Sep 26, 2013.

  1. Uri Guttman

    Uri Guttman Guest

    >>>>> "CD" == Cal Dershowitz <> writes:

    CD> sub get_config{
    CD> my $my_ftp = shift;
    CD> print "my ftp is $my_ftp\n";

    sub code should be indented. i can't read this at all. you write code
    for the reader, not yourself.


    CD> my $ident = 'template_stuff/ident.txt';
    CD> my $config = do($ident);

    first time i have seen do( file ) in ages. you might be better off
    slurping in yourself and calling eval on it. same results with better
    control.


    CD> unless ($config) {
    CD> die("read error: $!") if $!;
    CD> die("parse error: $@") if $@;

    you can't have $! as you didn't do a system call yourself. it may be set
    and left over which is bogus. if the do fails it will die and you won't
    even get to that code.


    CD> }
    CD> $domain = $config->{$my_ftp};
    CD> die("unknown domain: $!") unless $domain;

    why are you printing $! there? you are just checking a value. that can't
    set $! which is only set by system calls. wasteful and misleading.

    CD> $domain = $config->{$my_ftp};

    CD> Why does this not assign to %domain in the subroutine?

    $domain and %domain are separate variables. don't confuse $domain{foo}
    which is a hash access with $domain which is a scalar variable.

    uri
    Uri Guttman, Sep 26, 2013
    #1
    1. Advertising

  2. Uri Guttman

    Uri Guttman Guest

    >>>>> "CD" == Cal Dershowitz <> writes:

    CD> unless ($config) {
    CD> die("read error: $!") if $!;
    CD> die("parse error: $@") if $@;
    >>
    >> you can't have $! as you didn't do a system call yourself. it may be set
    >> and left over which is bogus. if the do fails it will die and you won't
    >> even get to that code.


    CD> I won't be sad to see these lines disappear from my scripts going
    CD> forward at all.

    i don't get what sad has to do with it. the $! lines you have tell you
    nothing. absolutely nothing. they are ONLY for when you make a system
    call like open/close, etc. you are not making such a call so there is no
    information in $! for you.

    >>
    >>

    CD> }
    CD> $domain = $config->{$my_ftp};
    CD> die("unknown domain: $!") unless $domain;
    >>
    >> why are you printing $! there? you are just checking a value. that can't
    >> set $! which is only set by system calls. wasteful and misleading.


    CD> same comment.

    same comment. lose the $! lines now.

    >>

    CD> $domain = $config->{$my_ftp};
    >>

    CD> Why does this not assign to %domain in the subroutine?
    >>
    >> $domain and %domain are separate variables. don't confuse $domain{foo}
    >> which is a hash access with $domain which is a scalar variable.
    >>
    >> uri
    >>


    CD> The word domain was overloaded in a way that barely made sense to me.
    CD> I believe what I endeavor to do here is called auto-vivification. I'm
    CD> not sure what separate the auto- version of it from the garden-variety
    CD> vivifying, but that goes precisely to my question as to how the hash
    CD> should appear in ident1.txt. To my eye, it looks like the treatments
    CD> I've seen.

    no it isn't autovivication. you are just confusing a scalar and a
    hash. it is a good practice not to use the same names for different
    variable types. $domain and %domain_info or something like that. generic
    names are not good in any case.

    CD> Q1) What do I have to change about this identity file to make it
    CD> kosher for vivifying in this manner?

    i don't know what is going on but it isn't anything to do with
    autovivification. you are using the term but you don't know what it
    means. it has to do with perl creating references for you when you imply
    there is one via a deep access.

    uri
    Uri Guttman, Sep 26, 2013
    #2
    1. Advertising

  3. On 9/26/2013 4:48 PM, Ben Morrow wrote:
    >
    > ...
    >
    >> CD> unless ($config) {
    >> CD> die("read error: $!") if $!;
    >> CD> die("parse error: $@") if $@;
    >>
    >> you can't have $! as you didn't do a system call yourself. it may be set
    >> and left over which is bogus. if the do fails it will die and you won't
    >> even get to that code.

    >
    > ...because do FILE can set either $! or $@. It sets $! if reading the
    > file fails, and $@ if the file can be read but the eval fails. (However,
    > Cal is checking them in the wrong order: the documentation is explicit
    > that if $@ is set then $! may also be set to something spurious, so $@
    > should be checked first.)
    >


    I notice the doc example grows messy:

    unless ($return = do $file) {
    warn "couldn't parse $file: $@" if $@;
    warn "couldn't do $file: $!" unless defined $return;
    warn "couldn't run $file" unless $return;
    }


    I'd be tempted to just write something like:

    $return = do $file or warn "do: ",$@ || $! || "return value?";

    --
    Charles DeRykus
    Charles DeRykus, Sep 27, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    493
    red floyd
    Nov 10, 2011
  2. JWhite

    Perl CGI utilities?

    JWhite, Jul 1, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    269
    Todd Wade
    Jul 5, 2008
  3. Ben Bacarisse

    Re: perl hash utilities

    Ben Bacarisse, Sep 26, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    165
    Ben Bacarisse
    Sep 26, 2013
  4. George Mpouras

    Re: perl hash utilities

    George Mpouras, Sep 26, 2013, in forum: Perl Misc
    Replies:
    9
    Views:
    210
    John W. Krahn
    Oct 6, 2013
  5. Rainer Weikusat

    Re: perl hash utilities

    Rainer Weikusat, Oct 4, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    136
    Rainer Weikusat
    Oct 4, 2013
Loading...

Share This Page