CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
hash K/V slice not operating per docs.

 



bc3-au
New User

Feb 7, 2017, 6:47 PM

Post #1 of 3 (1101 views)
hash K/V slice not operating per docs. Can't Post

I'm having problems with using a hash key/value slice, as per the perldata manual.

I have taken the example directly from the manual and it won't compile:


Quote
# Example from perldata:

%h = (blonk => 2, foo => 3, squink => 5, bar => 8);
%subset = %h{'foo', 'bar'}; # key/value hash slice
# %subset is now (foo => 3, bar => 8)

root@devsm1-per:/Structerre/sm/lib-TestSuite# perl 1.pl
syntax error at 1.pl line 3, near "%h{"
Execution of 1.pl aborted due to compilation errors.

root@devsm1-per:/Structerre/sm/lib-TestSuite# perl -v

This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi


I have tried other examples of this form of slice & see the same issue.

The manual states that this syntax is supported as of Perl-5.20
http://perldoc.perl.org/perldata.html#Slices

I can quite easily create a slice using map and other tools, however this syntax is cleaner & more readable when I'm passing selected parameters between calls:


Quote
sub foo {
my($obj, %pars)= @_;
[..]
$frotz= $obj->bar(%pars{qw/DB table key tansid pivot/});



BillKSmith
Veteran

Feb 7, 2017, 8:53 PM

Post #2 of 3 (1096 views)
Re: [bc3-au] hash K/V slice not operating per docs. [In reply to] Can't Post

You need a newer version of perl. Let me rephrase what the manual says: "This syntax is not supported in versions of perl older than v5.20."

I recommend that you read perl documentation with the utility perldoc. This ensures that you are reading the documents that came with your installation of perl. You can get instructions by typing at your command line:

Code
perldoc perldoc

Good Luck,
Bill


bc3-au
New User

Feb 7, 2017, 11:26 PM

Post #3 of 3 (1089 views)
Re: [BillKSmith] hash K/V slice not operating per docs. [In reply to] Can't Post

Thanks Bill, that was the issue: it's an interesting way the docs state compatibility.

I assume this is an issue with how the various distributions manage and number their packages.

This system is old enough that I'm finding problems with various CPAN modules also. unfortunately upgrading isn't an option at this point.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives