views:

131

answers:

4

Say I make an object as follows:

$object22=new somepackage("stuff");

and later I want to run a subroutine like this:

$object22->somesubroutine();

I would like to capture the string "object22" in the subroutine "somesubroutine." I tried:

$self=@_;
print $self;

but that just gave me somepackage=HASH(somehexnumber)

Please let me know if this is possible and if so what the code is to do so.

+7  A: 

This is impossible without horrible hackery (looking through the stack frames). The name is just a reference to the object, it is not part of the object.

You're approaching the problem in the wrong way. Perhaps describe it more?

Forrest
Thanks for saving me the time. I am using this object to write input files from a template. Each object essentially contains strings to search for, strings to replace these strings with, and a text file to look in. And of course subroutines that do all this. I would like to organize the input files generated. I thought what better way to do this than to store the input files in directories named after the object that created them. I intend to send these input files to programs that read them and create output files, so I can store all these in one nicely named directory.
Feynman
+1  A: 

This is possible with horrible hackery that crawls through the stack frames. Something like PadWalker, perhaps.

But you're probably approaching the problem wrong.

mobrule
Thanks for saving me the time. I am using this object to write input files from a template. Each object essentially contains strings to search for, strings to replace these strings with, and a text file to look in. And of course subroutines that do all this. I would like to organize the input files generated. I thought what better way to do this than to store the input files in directories named after the object that created them. I intend to send these input files to programs that read them and create output files, so I can store all these in one nicely named directory.
Feynman
This is amazingly similar to my answer ... o.O
Forrest
+3  A: 

I can think of two ways to get around your problem:

  1. make the name you're interested in a attribute of the object
  2. store your objects in a hash, using the name as the key

Paul

pavel
A: 

An alternative to using the stack frames is using caller () to get the line your routine was called from and then read the program file and get the variable name like that.

#!/usr/local/bin/perl
use warnings;
use strict;
package X;
sub new
{
    bless {};
}
sub getdownonthatcrazything
{
    my ($self) = @_;
    my (undef, $file, $line) = caller ();
    open my $in, "<", $file or die $!;
    while (<$in>) {
        if ($. == $line) {
            goto found;
        }
    }
    die "Something bad happened";
    found:
    if (/\$(\w+)\s*->\s*getdownonthatcrazything\s*\(\)/) {
        my $variable = $1;
        print "I was called by a variable called \$$variable.\n";
    }
    close $in or die $!;
}
1;
package main;
my $x = X::new ();
$x->getdownonthatcrazything ();
my $yareyousofunky = X::new ();
$yareyousofunky->getdownonthatcrazything ();

Output:

$ ./getmycaller.pl 
I was called by a variable called $x.
I was called by a variable called $yareyousofunky.

This assumes that your files are all in the same directory. If not you could use the FindBin module and search @INC for libraries, etc.

Kinopiko