Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use string ("Server1") as a SCALAR ref while "strict refs" in use

Tags:

perl

#!/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?

like image 519
Ryan Avatar asked Oct 17 '25 16:10

Ryan


1 Answers

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};
like image 156
choroba Avatar answered Oct 19 '25 12:10

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!