How To Count Lines In A String Variable Using Perl

1 |
my $count = () = $string =~ /\n/g; |
![]() |
1 |
my $count = () = $string =~ /\n/g; |
Solution: “Double Quote” your variables!
1 2 3 4 5 6 7 8 9 10 11 |
shell> myvar="abc def ghi" shell> echo $myvar abc def ghi shell> echo "$myvar" abc def ghi |
See Also: https://stackoverflow.com/questions/22101778/how-to-preserve-line-breaks-when-storing-command-output-to-a-variable
$merged_object = (object) array_merge((array) $object1, (array) $object2);
I wanted to call a sub-routine based on a variable in Perl, like this:
1 2 |
our $role = 'master'; &$role; |
but it failed because of use strict:
1 |
Can't use string ("master") as a subroutine ref while "strict refs" in use at /opt/continuent/tungsten/tools/tungsten_find_orphaned line 213. |
To enable using a variable as a reference, simply specify that to Perl:
1 2 |
use strict no strict "refs"; |
Inside class “yourClassA” calling “yourClassB”:
1 2 3 4 |
class yourClassA { $classObject = new yourClassB; $classObject->setClass($this); } |
Inside class “yourClassB” getting the call from “yourClassA”:
1 2 3 4 5 6 7 8 9 10 |
class yourClassB { public function setClass($object = NULL) { if(is_object($object) and $object instanceof yourClassA) { $this->RefToClassA = $object; $this->yourVar = $RefToClassA->otherVar; // etc, etc, etc... } } } |
1 2 3 4 5 6 7 8 9 |
public function valid($function = '') { $methodVariable = array($this, $function); if (is_callable($methodVariable)) { return $this->{$function}(); } else { $this->msgs[] = "Validation method $function is unavailable!"; return 0; } } |
1 2 3 4 5 6 7 |
jQuery('body').data('myGlobalVarName', { thePath: window.location.pathname, theHREF: jQuery(location).attr('href'), theTitle: jQuery(this).attr('title'), Foo: 'bar', hello: 'world', }); |