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

When I use CGI::Carp 'fatalsToBrowser', I would also like to get the header function from CGI.pm. But I would like to do so without having to use CGI again. How can I do this is in one use statement?

Also why are hash slices useful?

Replies are listed 'Best First'.
Re: using CGI, hash slices, and perldoc
by dragonchild (Archbishop) on Feb 02, 2005 at 03:31 UTC
    CGI and CGI::Carp are completely unrelated modules. The fact that CGI::Carp has the name CGI in it is for our purposes; Perl doesn't attach any meaning to that. So, you need two use statements.

    Hashslices are useful when you want to get/set the values for more than one key at a time. So, instead of

    my @values; push @values, $hash{$_} for qw( key1 key2 key2 );
    you can do something like
    my @values = @hash{ qw( key1 key2 key3 ) };

    Some people feel that hashslices are annoying to read. Others, like me, love them. No-one says you have to use them. If you like them, great. If you don't, great. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: using CGI, hash slices, and perldoc
by davido (Cardinal) on Feb 02, 2005 at 07:15 UTC

    I think the issue you're running into is that if CGI::Carp traps an error and attempts to output the message to the browser before your CGI script has had a chance to output proper CGI headers, you'll get that old familiar problem of attempting to generate output without proper headers.

    The solution is for your CGI::Carp handle_errors() subref to handle the outputting of headers too. You may find it helpful to keep a flag that lets you know whether or not you've already output a header. The error handling has to be established in a BEGIN{} block so that compiletime errors will properly be handled too.

    Regarding hash slices, consider the following examples:

    @hash{ @keynames } = @valuelist; # Assign values to a bunch # of keynames at once. @valuelist = @hash{ @keynames }; # Grab a bunch of values given # a list of keynames. ( $address, $phone ) = @{$people{ $name }}{ 'address', 'phone' }; # Grab individual fields # from a record.

    The usefulness of a language feature is just a matter of need and imagination.


    Dave