Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Pointer to a structure in Perl

by vishant (Initiate)
on Apr 01, 2013 at 06:53 UTC ( #1026431=perlquestion: print w/ replies, xml ) Need Help??
vishant has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Can we have pointer to a structure in Perl as we have in C language?

If yes, Can you provide some details about it. It would be highly appreciated if you provide some tutorial link on the same.

Thanks & Regards

Vishant/

Comment on Pointer to a structure in Perl
Re: Pointer to a structure in Perl
by BrowserUk (Pope) on Apr 01, 2013 at 07:03 UTC

    Perl doesn't do pointers or structures.

    You can have a reference to a hash which is functionally equivalent.

    But you'd be better off describing what you are trying to achieve.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Pointer to a structure in Perl
by Loops (Hermit) on Apr 01, 2013 at 07:46 UTC
    Sounds like you're looking for Perl references. Check out the perlref and perlreftut pages using the perldoc command. As a quick example:
    my @array = qw( hello world ); my $array_ref = \@array; print $array[0], "\n"; # prints hello print $array_ref->[0], "\n"; # prints hello

      ... and then check out perldsc and perllol to get the structure part.

Re: Pointer to a structure in Perl
by BillKSmith (Chaplain) on Apr 01, 2013 at 13:06 UTC

    Perl references are similar to C pointers unless you are working with strings. A perl string in a scalar, not an array of characters as in C. One of perl's greatest strengths is its string manipulation, but note that it is quite different from C. There is no such thing as a pointer/reference to a charater withing a string.

    Bill
Re: Pointer to a structure in Perl
by sundialsvc4 (Monsignor) on Apr 01, 2013 at 19:08 UTC

    Indeed, the secret-sauce that you are looking for here is “references.”

    The concept here is several levels above the concept that you see in “C,” this being of course the language in which Perl was implemented.   A reference is, if you want to think of it this way, “a pointer to” a complete, self-describing Thing, that is also well-known to the Perl interpreter and fully part of the environment that it creates.   Perl can look at a reference , and know everything that needs to be known about what it refers to.   It knows how many references currently exist to that same thing, therefore it knows whether-or-not that object is eligible to be garbage-collected.   The Perl language does not have the concept of a “structure,” in the C/C++ sense, but it does have the concept of a list, array, and hash.

    The environment that has been created by the Perl interpreter is intended to focus upon providing an efficient solution to the same problem that one might use “pointers to structures” in bare-bones C to address, but to do so in a way that is much less reliant upon the program-in-question to “do everything in just the right way, or else.”   There exist several layers of well-tested software that any Perl programmer can rely upon the existence of, that a C-programmer cannot.   All of us here fully-understand both environments.

Re: Pointer to a structure in Perl
by bulk88 (Priest) on Apr 02, 2013 at 03:55 UTC
    pack, look for letter 'P', the pointer is useless (no core func or operator other than unpack and syscall will take it) in Perl except for syscall. You probably want to read perlxstut.
Re: Pointer to a structure in Perl
by DrHyde (Prior) on Apr 02, 2013 at 10:21 UTC

    Other people have wibbled about references, but in case you're trying to deal with data in some machine-specific C-generated format, or to generate data to pass to such (believe it or not there are some crazy programs that expect you to drive them by dropping a C struct in a file, or even pass them a struct on STDIN), then you might want Convert::Binary::C.

    I've not used it myself, but trying it out is on my to-do list for solving one of those thorny problems.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026431]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-07-30 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls