I have a struct and would like to print out the contents within quotes
#!/usr/bin/perl
use Class::Struct;
struct( astruct => [ test => '$']);
my $blah = new astruct;
$blah->test("asdf");
print "prints array reference: '$blah->test'\n";
print "prints content: '", $blah->test, "'\n";
The output is
prints array reference: 'astruct=ARRAY(0x20033af0)->test'
prints content: 'asdf'
Is there a way to print the contents within the quotes?
Its making my code a bit scruffy having to open and close quotes all the time. It’s also problematic when using “ to run commands which use the contents of structs.
The variable
$blahholds an array reference and is interpolated into the string before it can be dereferenced. To change that, we put the dereferencing either outside the String:or pull a little trick with an anonymous array:
We dereference (
@{...}) an anonymous array ([...]) which we construct from the return value of thetestmethod. (Our your struct field, whatever.)While both these methods work when constructing a string, the second form can easily be used in a
qxor backticks environment. You could also build a string$commandand then execute that withqx($command).If you don’t need the additional functionality of the
Class::Struct, you can always use hashes and spare yourself the hassle:or