4
#!/usr/bin/perl -w
use strict;
use warnings;

use Class::Struct;

struct System => {
  Name => '$',
};

my $system = new System;
$system->Name("Server1");

my $strout1 = qq{Server is ${$system->Name}\n};
my $strout2 = "Server is \"".$system->Name."\"\n";

print $strout1;
print $strout2;

results in:

Can't use string ("Server1") as a SCALAR ref while "strict refs" in use at test.pl line 14.

I want to be able to use qq and deref $system->Name correctly. Can anyone explain where i am going wrong?

toolic
  • 57,801
  • 17
  • 75
  • 117
Ryan
  • 219
  • 3
  • 12

2 Answers2

7

Method calls aren't interpolated in double quoted strings, but dereferences are. If you want to interpolate the result of the method call, you must dereference a reference to it:

my $strout1 = qq{Server is ${\$system->Name}\n};
choroba
  • 231,213
  • 25
  • 204
  • 289
1

Does the Name method really return a reference? Because this looks wrong:

${$system->Name}

That's dereferencing something, so I think should be written as simply $system->Name

You'll trigger the same error if you try:

print ${"Server1"};

Which suggests that you are actually getting a text string back.

Sobrique
  • 52,974
  • 7
  • 60
  • 101