LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2006, 10:43 AM   #1
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Rep: Reputation: 15
unblessed references in lists in Perl


Hello,

I wonder what it would be the right way to treat anonymous references in lists. I have a list like this:

Code:
$element = ['one', 'two', {'3' => 'three', '4' => 'four'}, 'five'];
and when in a loop I want to print it, I get the error "Can't call method "treat" on unblessed reference":

Code:
treat( $element);

sub treat {
  @array = @{$_[0]} if ref( $_[0]) eq "ARRAY";
  @array = values %{$_[0]} if ref( $_[0]) eq "HASH";

  for( @array) {
    if( ref( $_) eq "HASH") {
      treat $_;
    }
    else {
      print "$_ ";
    }
  }
};
I get 'one two' and the error message, and not the 'one two three four five'.

Thank you in advance.
 
Old 06-21-2006, 12:43 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Is this in a package file? Is there another method somewhere also called treat? Sounds like perl thinks treat is a method call, which would only make sense to call with an instance of the object created by the package. It seems odd that it wouldn't error at your first call to treat, because the array ref is also unblessed. The only difference between the two calls is the parentheses...
 
Old 06-21-2006, 01:09 PM   #3
thelonius
Member
 
Registered: Aug 2005
Location: Montréal
Distribution: Debian Testing, Slackware 10.2
Posts: 136

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by puffinman
The only difference between the two calls is the parentheses...
...and this was the error not to use parentheses for the second call. Now it works.

Here is the complete correct version (pay attention to the 'my' declaration of the inner array):

Code:
#!/usr/bin/perl
$element = ['one', 'two', {'3' => 'three', '4' => 'four'}, 'five'];
treat( $element);
sub treat {
  my @array;
  @array = @{$_[0]} if ref($_[0]) eq "ARRAY";
  @array = values %{$_[0]} if ref($_[0]) eq "HASH";
  for (@array) {
    if( ref( $_) =~ /(HASH)|(ARRAY)/) {
      treat( $_);
    }
    else {
      print "$_ ";
    }
  }
}
Thanksssssss!!!!!!!!

Last edited by thelonius; 06-21-2006 at 01:11 PM.
 
  


Reply

Tags
perl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl: easiest way to extract scalars from lists? apeekaboo Programming 4 10-27-2005 12:27 AM
method references in perl mschutte Programming 4 04-26-2005 09:46 AM
perl: passing 2 individual arrays/lists as args johnMG Programming 12 04-18-2005 03:49 PM
regular expressions and back references within [] lists Tramontane Programming 1 12-19-2003 01:53 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration